<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/main.css" />
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/jquery.mousewheel.js"></script>
    <script src="./js/axios.js"></script>
    <title>聊天机器人</title>
  </head>

  <body>
    <div class="wrap">
      <!-- 头部 Header 区域 -->
      <div class="header">
        <h3>小思同学</h3>
        <img src="img/person01.png" alt="icon" />
      </div>
      <!-- 中间 聊天内容区域 -->
      <div class="main">
        <ul class="talk_list" style="top: 0px">
          <li class="left_word">
            <img src="img/person01.png" /> <span>你好</span>
          </li>
          <li class="right_word">
            <img src="img/person02.png" /> <span>你好哦</span>
          </li>
          <li class="left_word">
            <img src="img/person01.png" /> <span>你好</span>
          </li>
          <li class="right_word">
            <img src="img/person02.png" /> <span>你好哦</span>
          </li>
          <li class="left_word">
            <img src="img/person01.png" /> <span>你好</span>
          </li>
          <li class="right_word">
            <img src="img/person02.png" /> <span>你好哦</span>
          </li>
          <li class="left_word">
            <img src="img/person01.png" /> <span>你好</span>
          </li>
          <li class="right_word">
            <img src="img/person02.png" /> <span>你好哦</span>
          </li>
          <li class="left_word">
            <img src="img/person01.png" /> <span>你好</span>
          </li>
          <li class="right_word">
            <img src="img/person02.png" /> <span>你好哦</span>
          </li>
          <li class="left_word">
            <img src="img/person01.png" /> <span>你好</span>
          </li>
          <li class="right_word">
            <img src="img/person02.png" /> <span>你好哦</span>
          </li>
        </ul>
        <div class="drag_bar" style="display: none">
          <div
            class="drager ui-draggable ui-draggable-handle"
            style="display: none; height: 412.628px"
          ></div>
        </div>
      </div>
      <!-- 底部 消息编辑区域 -->
      <div class="footer">
        <img src="img/person02.png" alt="icon" />
        <input type="text" placeholder="说的什么吧..." class="input_txt" />
        <input type="button" value="发 送" class="input_sub" />
      </div>
    </div>
    <audio src="" autoplay style="display: none"></audio>
    <script type="text/javascript" src="js/scroll.js"></script>
    <script>
      $(function () {
        // 初始化右侧滚动条
        // 这个方法定义在scroll.js中
        resetui();
      });

      axios.defaults.baseURL = "http://www.itcbc.com:3006";
      // 1. 点击按钮,获取输入框的值,并渲染到页面中
      document
        .querySelector(".input_sub")
        .addEventListener("click", async function () {
          let val = document.querySelector(".input_txt").value;
          // 判断输入内容是否为空
          if (val === "") return;
          // 如果不为空,渲染
          let li = ` <li class="right_word">
            <img src="img/person02.png" /> <span>${val}</span>
          </li>`;
          // 将li加入到ul的底部
          document.querySelector(".talk_list").innerHTML += li;
          resetui();
          document.querySelector(".input_txt").value = "";

          // 2. 调用接口,获取机器人的回复,渲染页面
          const { data: res } = await axios.get("/api/robot", {
            params: { spoken: val },
          });
          let robot = res.data.info.text;
          let li2 = ` <li class="left_word">
            <img src="img/person01.png" /> <span>${robot}</span>
          </li>`;
          // 将li加入到ul的底部
          document.querySelector(".talk_list").innerHTML += li2;
          resetui();

          // 3. 将机器人回复,转换为语音
          const { data: res1 } = await axios.get("/api/synthesize", {
            params: { text: robot },
          });
          let voice = res1.voiceUrl;
          document.querySelector("audio").src = voice;
        });

      // 4. 优化 按回车触发按钮发送的点击事件
      document
        .querySelector(".input_txt")
        .addEventListener("keyup", function (e) {
          if (e.key === "Enter") {
            document.querySelector(".input_sub").click();
          }
        });

      console.log(111);
    </script>
  </body>
</html>
